package com.four.project.service.impl;

import com.four.project.dao.UserMapper;
import com.four.project.pojo.User;
import com.four.project.service.UserService;
import com.four.project.util.RandomUtil;

import com.four.project.vo.UserReq;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Description
 * @Author 梯子君(xie)
 * @Data 2020/7/615:24
 */

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public PageInfo selectAll(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<User> all = userMapper.selectAll();
        //得到条数和分页内容
        PageInfo pageInfo = new PageInfo(all);
        return pageInfo;
    }

    @Override
    public User selectById(Integer id) {
        try {
            User user = userMapper.selectById(id);
            return user;
        }catch (Exception e){
            System.out.println( e.getMessage());
            return null;
        }
    }

    @Override
    public int addOne(User user) {
        int i = userMapper.addOne(user);
        return i;
    }

    @Override
    public int deleteById(Integer id) {
        int i = userMapper.deleteById(id);
        return i;
    }

    @Override
    public int updateOne(User user) {
        int i = userMapper.updateOne(user);
        return i;
    }

    /**
     * 验证邮件的发送
      * @param email
     */
    @Autowired
    JavaMailSender javaMailSender;//邮件对象
    @Value("${spring.mail.username}")
    private String from;
    @Override
    public void sendMail(String email ,HttpServletRequest request) {
        //创建邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            //将邮件对象放入mimeMessageHelper(消息代理对象)
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
            //发件人
            mimeMessageHelper.setFrom(from);
            //收件人
            mimeMessageHelper.setTo("121960185@qq.com");
            //题目
            mimeMessageHelper.setSubject("测试发送验证邮件");
            //主体内容
            String num =RandomUtil.randomInteger();
            HttpSession session = request.getSession();
            session.setAttribute(email,num);
            mimeMessageHelper.setText("<font stule='color:red'>"+num+"</font>",true);
            //发送
            javaMailSender.send(mimeMessage);
        }catch (MessagingException e) {
            e.printStackTrace();
        }

    }

    /**
     * 登录
     * @param userReq
     */
    @Override
    public int login(UserReq userReq,HttpServletRequest request) {
        String email = userReq.getEmail();
        User user = userMapper.selectByEmail(email);
        if (user==null){//判断数据库中是否
            return 0;
        }

        String password = user.getPassword();
        if (!userReq.getPassword().equals(password)){//密码是否相等
            return 0;
        }
        HttpSession session = request.getSession();
        session.setAttribute("user",user);
        return 1;
    }

    /**
     * 登录验证
     * @return
     */
    @Override
    public User findUserByLogin(String email) {

        return userMapper.selectByEmail(email);
    }
}
